package se.gui;

import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import se.kernel.Contacto;

public class PanelContactos extends JPanel
{

    private static final long serialVersionUID = 1L;
    private JList listaContactos = null;
    private VentanaPrincipal principal;
    private JButton btnIniciarConversacion = null;
    /**
     * This is the default constructor
     */
    public PanelContactos( VentanaPrincipal vp )
    {
        super( );
        initialize( );
        principal = vp;
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize( )
    {
        this.setSize( 576, 295 );
        this.setLayout( null );
        this.setBorder( BorderFactory.createTitledBorder( null, "Contactos", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null ) );
        JScrollPane scroll = new JScrollPane( getListaContactos( ) );
        scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
        scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
        scroll.setBounds( new Rectangle( 19, 20, 537, 235 ) );
        this.add( scroll, null );
        this.add( getBtnIniciarConversacion( ), null );
    }

    /**
     * This method initializes listaContactos
     * 
     * @return javax.swing.JList
     */
    private JList getListaContactos( )
    {
        if( listaContactos == null )
        {
            listaContactos = new JList( );
            listaContactos.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        }
        return listaContactos;
    }

    public void refrescarLista( ArrayList<Contacto> contactos )
    {
        listaContactos.setListData( contactos.toArray( ) );
        listaContactos.clearSelection( );
    }

    /**
     * This method initializes btnIniciarConversacion
     * 
     * @return javax.swing.JButton
     */
    private JButton getBtnIniciarConversacion( )
    {
        if( btnIniciarConversacion == null )
        {
            btnIniciarConversacion = new JButton( );
            btnIniciarConversacion.setBounds( new Rectangle( 396, 262, 154, 26 ) );
            btnIniciarConversacion.setText( "Iniciar conversacion" );
            btnIniciarConversacion.addActionListener( new java.awt.event.ActionListener( )
            {
                public void actionPerformed( java.awt.event.ActionEvent e )
                {
                    if( listaContactos.getSelectedValue( ) != null )
                    {
                        Contacto contacto = ( Contacto )listaContactos.getSelectedValue( );
                        principal.iniciarConversacion( contacto.getUsuario( ) );
                    }
                }
            } );
        }
        return btnIniciarConversacion;
    }

} // @jve:decl-index=0:visual-constraint="10,10"
